<?php
declare (strict_types = 1);

namespace app\admin\model\project;

use bbweb\traits\ModelTrait;
use bbweb\basic\BaseModel;
use think\facade\Session;

/**
 * @mixin \think\Model
 */
class ProjectTaskLogs extends BaseModel
{
    /**
     * 数据表主键
     * @var string
     */
    protected $pk = 'id';

    /**
     * 模型名称
     * @var string
     */
    protected $name = 'project_task_logs';

    // 设置json类型字段
    protected $json = ['data'];
    // 设置JSON数据返回数组
    protected $jsonAssoc = true;

    use ModelTrait;


    // 设置字段信息
    protected $schema = [
        'id'			=> 'int',
        'project_id'    => 'int',
        'task_id'	    => 'int',
        'uid'		    => 'int',
        'user_name'	    => 'varchar',
        'event_name'	=> 'varchar',
        'data'          => 'text',
        'create_at'     => 'int',
    ];

    public function getCreateAtAttr($value)
    {
        return date('Y/m/d H:i', $value);
    }

    /**
     * 添加日志
     * @param $uid
     * @param $project_id
     * @param $task_id
     * @param $event_name
     * @param $events
     * @return ProjectTaskLogs|\think\Model
     */
    public static function addLogs($project_id, $task_id, $event_name, $events)
    {
        $adminInfo = Session::get('adminInfo');
        $item = [
            'project_id'    => $project_id,
            'task_id'	    => $task_id,
            'uid'		    => $adminInfo['id'],
            'user_name'		=> $adminInfo['real_name'],
            'event_name'	=> $event_name,
            'data'          => $events,
            'create_at'     => time(),
        ];
        return self::create($item);
    }
}
